/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
import program from 'commander';
import { VERSION } from './utils/constants';
import apply from './index';

// 多种功能命令
const actionMap = {
  install: { // 配置命令的名字
    alias: 'i', // 命令的别名
    description: 'install template', // 命令对应的描述
    examples: [ // 命令对应的模板
      'zf-cli i',
      'zf-cli install',
    ],
  },
  config: {
    alias: 'c',
    description: 'config .zfclirc',
    examples: [
      'zf-cli config set <k> <v>',
      'zf-cli config get <k>',
      'zf-cli config remove <k>',
    ],
  },
  '*': {
    description: ' command not found',
    examples: [],
  },
};
// 遍历命令配置及别名等信息
Object.keys(actionMap).forEach((action) => {
  program.command(action)
    .description(actionMap[action].description || '')
    .alias(actionMap[action].alias || '')
    .action(() => {
      // console.log(action);
      // 判断一下当前用的哪个操作
      if (action === 'config') {
        // 实现更改配置文件
        apply(action, process.argv.slice(3));
      } else if (action === 'install') {
        console.log(action);
      }
      apply();
    });
});

function help () {
  // console.log('123') // 吧example 显示出来
  console.log('\r\n  how to use command');
  Object.keys(actionMap).forEach((action) => {
    actionMap[action].examples.forEach((example) => {
      console.log(`   - ${example}`);
    });
  });
}
program.on('-h', help);
program.on('--help', help);
program.version(VERSION, '-v --version').parse(process.argv);
